package com.maycurobj.configuration;

import com.maycurobj.controller.maycurbase.SuperiorSynchronization;
import com.maycurobj.pojo.EhrEmployeeInfo;
import com.maycurobj.service.impl.DingTalkService;
import com.maycurobj.service.impl.EhrService;
import com.maycurobj.service.impl.MaycurService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 定时任务标记类
 */
@Configuration
@EnableScheduling
@Slf4j
public class CronTask {

    @Autowired
    private EhrService ehrService;
    @Autowired
    private MaycurService maycurService;
    @Autowired
    private SuperiorSynchronization superiorSynchronization;
    @Autowired
    private DingTalkService dingTalkService;
    /**
     * 测试用 定时任务 (每 10秒一次)
     */
//    @Scheduled(cron = "0/10 * * * * ?")
//    private void demoCronTask(){
//        System.out.println("定时任务Demo" + LocalDateTime.now());
//    }

    /**
     * 0 08 02 ? * *
     */
    @Scheduled(cron = "0 08 02 ? * *")
    private void syncEhrToMayCurUserInfo(){
        log.info("定时任务，开始同步ehr userinfo to maycur" +LocalDateTime.now());
        Map<String, Object> ehrUserObject = ehrService.getEHRUserInfo();
        List<EhrEmployeeInfo> ehrItemList=(List<EhrEmployeeInfo>)ehrUserObject.get("ehrObject");
        maycurService.employEhrToMayCur(ehrItemList);
        log.info("定时任务，结束同步ehr userinfo to maycur" +LocalDateTime.now());
    }
    //已经迁移到BPM中
    private void updateSuperiorSynchronization(){
        log.info("定时任务，开始同步上级"+LocalDateTime.now());
        superiorSynchronization.timedSuperior();
        log.info("定时任务，结束同步上级"+LocalDateTime.now());
    }

    @Scheduled(cron = "0 0 0/6 ? * ? ")
    private void handleLeaveMaycurSynchronization(){
        log.info("定时任务，开始处理每刻离职"+LocalDateTime.now());
        maycurService.handleEmployeeLeave();
        log.info("定时任务，结束处理每刻离职"+LocalDateTime.now());
    }
    @Scheduled(cron = "0 50 03 ? * *")
    private void handleDingTalkEmployeeResign(){
        log.info("定时任务，开始更新离职人员钉钉部门"+LocalDateTime.now());
        dingTalkService.handleEmployeeLeave();
        log.info("定时任务，结束更新离职人员钉钉部门"+LocalDateTime.now());
    }

    @Scheduled(cron = "0 0 9 ? * MON")
    private void weeklyReminder() {
        log.info("每周一提醒合同任务开始：" + LocalDateTime.now());
        // 这里可以放置提醒逻辑，例如调用提醒服务发送通知
        maycurService.contractManagementRemind();
        log.info("每周一提醒合同任务结束：" + LocalDateTime.now());
    }

}
